package com.apexwear.apexutils.ui;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.data.OffScreenData;
import com.apexwear.apexutils.databinding.ActivityScreenBinding;
import com.apexwear.apexutils.dialog.SelectDialog;
import com.apexwear.apexutils.utils.ShowToast;

public class ScreenOutActivity extends AppCompatActivity {
    ActivityScreenBinding dataBinding;
    String[] timeArray;
    SelectDialog selectDialog;
    int outTime;
    OffScreenData offScreenData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_screen);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        offScreenData = new ViewModelProvider(ScreenOutActivity.this).get(OffScreenData.class);
        dataBinding.rowOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timeArray == null) {
                    timeArray = new String[60];
                    for (int i = 0; i < 60; i++) {
                        timeArray[i] = (i + 1) + "";
                    }
                }
                if (selectDialog == null) {
                    selectDialog = new SelectDialog(ScreenOutActivity.this, timeArray, getString(R.string.unit) + "(" + getString(R.string.second) + ")");
                    selectDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            outTime = Integer.valueOf(mac);
                            dataBinding.screen.setText(mac + getString(R.string.second));
                        }
                    });
                }
                if (!selectDialog.isShowing()) {
                    selectDialog.show();
                }
            }
        });
        dataBinding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    offScreenData.setOffTime(outTime).observe(ScreenOutActivity.this, new Observer<Boolean>() {
                        @Override
                        public void onChanged(Boolean aBoolean) {
                            if (aBoolean) {
                                ShowToast.show(ScreenOutActivity.this, getString(R.string.set_success));
                            } else {
                                ShowToast.show(ScreenOutActivity.this, getString(R.string.set_fail));
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            offScreenData.getOffTime().observe(ScreenOutActivity.this, new Observer<Integer>() {
                @Override
                public void onChanged(Integer integer) {
                    outTime = integer;
                    dataBinding.screen.setText(integer + getString(R.string.second));
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        if (selectDialog != null) {
            selectDialog.destroyDialog();
            selectDialog = null;
        }
        timeArray = null;
        offScreenData = null;
        if (dataBinding != null) {
            dataBinding.unbind();
            dataBinding = null;
        }
        super.onDestroy();
    }
}
